package com.programmercarl.tree;

import java.util.ArrayList;

/**
 * https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/
 *
 * @author yulongtian
 * @create 2024-12-24 10:59
 */
public class Test19 {
    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    // 左右根
    public int getMinimumDifference(TreeNode root) {
        // 中序遍历应该是有序的
        ArrayList<Integer> list = new ArrayList<>();
        inorderTraversal(root, list);
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < list.size(); i++) {
            min = Math.min(min, Math.abs(list.get(i) - list.get(i - 1)));
        }
        return min;
    }

    // 左根右
    public void inorderTraversal(TreeNode root, ArrayList<Integer> inorder) {
        if (root == null) {
            return;
        }
        inorderTraversal(root.left, inorder);
        inorder.add(root.val);
        inorderTraversal(root.right, inorder);
    }

}
